#! /usr/bin/python
#                   _ooOoo_                     
#                  o8888888o                    
#                  88" . "88                    
#                  (| -_- |)                    
#                  O\  =  /O                    
#               ____/`---'\____                 
#             .'  \\|     |//  `.               
#            /  \\|||  :  |||//  \              
#           /  _||||| -:- |||||-  \             
#           |   | \\\  -  /// |   |             
#           | \_|  \---/  |   |             
#           \  .-\__  `-`  ___/-. /             
#         ___`. .'  /--.--\  `. . __            
#      ."" '<  `.___\_<|>_/___.'  >'"".      
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |       
#     \  \ `-.   \_ __\ /__ _/   .-` /  /       
#======`-.____`-.___\_____/___.-`____.-'======  
#                   `=---='                     
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
#         Buddha bless me for no bug            
#                                               
####################################################################################
#   Filename    :  thread.py                                                      
#   Author      :  Shawn Tan                                                        
#   Date        :  2017.2.22                                                        
#   Description :                                                                   
#   History     :  2017.2.22             creat the document            Shawn Tan    
####################################################################################

__author__ = 'ST-CAT'

from StarVar  import *
from StarInfo import *
import threading
import copy

class THREAD_CONTROL(threading.Thread): 

  def __init__(self, ThreadName, TYPE, option): 
    threading.Thread.__init__(self, name=ThreadName)
    self.func_map={}
    self.args_map={}
    self.Name=ThreadName
    self.Stop_Thread=False 
    self.Status='PENDING'
    self.p=TYPE()
    self.Done = False
    self.option = option

  def register(self, name, args):
    def func_wrapper(func):
      if self.option.Step: 
        print info006+name

      self.func_map[name]=func
      self.args_map[name]=args
    return func_wrapper

  def control(self, Done):
    self.Done = True

  def run(self):
    self.Status = 'RUN'
    if len(self.func_map): 
      for i in self.func_map.keys(): 
        if self.option.Step: 
          print info007+i+' in Thread : '+self.Name
          #print self.args_map[i]
        if 'return' in i:
          self.p=self.func_map[i](*self.args_map[i])
        else:
          self.func_map[i](*self.args_map[i])
      self.Stop_Thread=True
    self.Status='DONE'

  def stop(self):

    if self.option.Step: 
      print info008+self.Name
    return self.p

class THREAD(threading.Thread):
  '''runsim process package'''

  def __init__(self, ThreadName, TYPE, option): 
    threading.Thread.__init__(self, name=ThreadName)
    self.func_map={}
    self.args_map={}
    self.Name=ThreadName
    self.Stop_Thread=False 
    self.p=TYPE()
    self.option = option

  def register(self, name, args):
    def func_wrapper(func):
      if self.option.Step: 
        print info006+name

      self.func_map[name]=func
      self.args_map[name]=copy.deepcopy(args)
    return func_wrapper

  # for the scenario which can't use @decorator
  def push(self, name):
    if self.option.Step: 
      print info009+name

    self.func_map[name]=func
    self.args_map[name]=copy.deepcopy(list)
     

  def run(self):

    if len(self.func_map): 
      for i in self.func_map.keys(): 
        if self.option.Step: 
          print info007+i+' in Thread : '+self.Name
          #print self.args_map[i]
        if 'return' in i:
          self.p=self.func_map[i](*self.args_map[i])
        else:
          self.func_map[i](*self.args_map[i])
      self.Stop_Thread=True

  def stop(self):

    if self.option.Step: 
      print info008+self.Name
    return self.p

